package hci;

/**
 * @author COMBES Nicolas - LECHANTRE Arnaud
 * 
 * Project: 		ChatSystem
 * Class: 			UserListView
 * Parent Class:	JPanel
 * Called by: 		MainFrame
 * Function : 		Display the connected user list and enable starting discussions
 * 
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import main.ChatSystem;


/**
 * Pattern adapter "case 2" -> used to clean the code of the frames
 * 
 * <p>
 * <ul>
 * <li>Project: 	ChatSystemG3</li>
 * <li>Called by: 	MainFrame</li>
 * </ul>
 * </p>
 * 
 * @author COMBES Nicolas - LECHANTRE Arnaud
 * @see JFrame
 * @see ActionListener
 */
@SuppressWarnings("serial")
public class UserListView extends JPanel implements ActionListener{
	
	/**
	 * Label displaying the user login
	 */
	private JLabel lblLogin;					//label displaying the user login
	/**
	 * Disconnect button
	 */
	private JButton btnDisconnect;				//disconnect button
	/**
	 * Start conv button
	 */
	private JButton btnStartConversation;		//start conversation button
	/**
	 * List of connected users
	 */
	private DefaultListModel userList;	//List of connected users
	/**
	 * View of the connected users List
	 */
	private JList connectedUserList;	//View of the connected users List
	/**
	 * Controller of the application (see MVC pattern)
	 */
	//TODO est-ce vraiment un controller?
	private FromUser chatGui;					//Controller of the application (see MVC pattern)
	/**
	 * Panel layout
	 */
	private GridBagLayout layout;				//Layout of the panel
	/**
	 * Label for userlist
	 */
	private JLabel lblUserlist;					//Label anouncing the userlist

	/**
	 * construct
	 * @param chatGui : Controller of the application
	 */
	public UserListView(FromUser chatGui) {
		//Initialize the fields
		this.chatGui=chatGui;
		this.lblLogin = new JLabel(ChatGui.strings.getString("loginText"));
		this.lblLogin.setPreferredSize(new Dimension(150,20));
		
		//Initialization of disconnect btn
		this.btnDisconnect= new JButton(ChatGui.strings.getString("disconnectBtnText"));
		this.btnDisconnect.addActionListener(this);
		this.btnDisconnect.setPreferredSize(new Dimension(150,20));
		
		//Initialize the fields
		this.chatGui=chatGui;
		this.lblUserlist = new JLabel(ChatGui.strings.getString("userlistText"));
		this.lblUserlist.setPreferredSize(new Dimension(150,20));
		
		//JList Initialization
		userList= new DefaultListModel();
		this.connectedUserList= new JList(userList);
		
		//Start Conversation btn Initialization
		this.btnStartConversation= new JButton(ChatGui.strings.getString("startConversationBtnText"));
		this.btnStartConversation.addActionListener(this);
		this.btnStartConversation.setPreferredSize(new Dimension(150,20));
		
		//Initialize the layout
		this.layout = new GridBagLayout();
		this.setLayout(layout);
		
		//2nd we initialize the constraint
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		//And add the components
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth=1;
		this.add(lblLogin, c);
		
		c.gridx++;
		this.add(btnDisconnect,c);
		
		c.gridx=0;
		c.gridy++;
		c.gridwidth=2;
		c.insets.top=10;
		c.fill= GridBagConstraints.BOTH;	
		this.add(lblUserlist,c);
		
		c.gridy++;
		c.weighty=1;
		c.insets.top=0;
		this.add(connectedUserList,c);
		
		c.weighty=0;
		c.gridy++;
		c.insets.top=10;
		this.add(btnStartConversation, c);
		

		//WE ADD SOME COLOUR :)
		this.setBackground(new Color(203, 231, 245));
	}

	/**
	 * @see ActionListener
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(this.btnDisconnect)){
			//If disconnect button has been pressed
			//Notify ctrl that want to disconnect
			this.chatGui.disconnect();
		} else if (arg0.getSource().equals(this.btnStartConversation)){
			//Else if the start conversation button has been pressed
			//We get selected uses
			int[] ind = this.connectedUserList.getSelectedIndices();
			String[] s = new String[ind.length];
			for (int i=0;i<ind.length;i++){
				s[i]=this.userList.get(ind[i]).toString();
			}
			//And notify the controller
			this.chatGui.startConversation(s);
			//Finally we clear the selection
			if(ChatSystem.debugMode)System.out.println("hci.UserListView :Oppening conversation");
			this.connectedUserList.clearSelection();
		}
	}
	
	/**
	 * Adds username to the connected user list
	 * @param username of the user to add
	 */
	public void addUserToUserList(String username){
		this.userList.insertElementAt(username, userList.getSize());
		this.connectedUserList.ensureIndexIsVisible(userList.getSize()-1);
	}
	
	/**
	 * Removes username from the connected user list
	 * @param username name of the user to be removed
	 */
	public void removeUserFromUserList(String username){
		
		for(int i=0;i<this.userList.size();i++){
			if(this.userList.get(i).toString().equals(username)){
				this.userList.remove(i);
				this.connectedUserList.repaint();
			}
		}
	}

	
	/**
	 * Sets the name of the local user
	 * @param username : username of the local user
	 */
	public void setUsername(String username) {
		this.lblLogin.setText(username);
	}

	/**
	 * flush
	 * 
	 * Action: empty the connected userlist
	 */
	public void flush() {
		this.userList.removeAllElements();
	}

	/**
	 * Changes the tongue of the panel by geting the new Strings saved in the ressource bundle
	 */
	public void changeTongue() {
		this.lblLogin.setText(ChatGui.strings.getString("loginText"));
		this.btnDisconnect.setText(ChatGui.strings.getString("disconnectBtnText"));
		this.btnStartConversation.setText(ChatGui.strings.getString("startConversationBtnText"));
	}

}
